package studyguide.ch09.input;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class LowLevelInputMidlet extends MIDlet {

	private final Command exitCommand;
	private final Display display;
	
	private final InputCanvas inputCanvas;
	
	private class SampleCommandListener implements CommandListener {

		public void commandAction(Command command, Displayable displayable) {
			if (command == exitCommand) {
				destroyApp(true);
				notifyDestroyed();
			} else {
				throw new RuntimeException("UNKNOWN COMMAND" + command);
			}
		}
		
	}
	
	public LowLevelInputMidlet() throws IOException {
		inputCanvas = new InputCanvas();
		
		exitCommand = new Command("Exit", Command.EXIT, 1);
		
		inputCanvas.addCommand(exitCommand);
		
		SampleCommandListener listener = new SampleCommandListener();
		inputCanvas.setCommandListener(listener);
		display = Display.getDisplay(this);
		
		// Displays information about the screen
		System.out.println("Double buffering support:" + inputCanvas.isDoubleBuffered());
		
		// Set to run full-screem
		inputCanvas.setFullScreenMode(true);
	}
	
	protected void destroyApp(boolean unconditional) {
		// No Op
	}

	protected void pauseApp() {
		// No Op
	}

	protected void startApp() throws MIDletStateChangeException {
		display.setCurrent(inputCanvas);
	}

}
